package com.gx.mxbbx.controller;

import com.github.pagehelper.PageInfo;
import com.gx.mxbbx.po.Disease;
import com.gx.mxbbx.po.Reimbursement;
import com.gx.mxbbx.po.Rural;
import com.gx.mxbbx.service.ReimbursementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.swing.*;

@Controller
public class ReimbursementController {

    @Autowired
    private ReimbursementService reimbursementService;

    @RequestMapping("/getReimList")
    public String getReimList(Integer currentPage, Integer limit, HttpServletRequest request, Model model) {
        String fullName = request.getParameter("context");
        currentPage = currentPage != null ? currentPage : 1;
        limit = limit != null ? limit : 10;

        PageInfo<Reimbursement> pageInfo = reimbursementService.getReimList(fullName, currentPage, limit);

        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("context", fullName);
        return "/reimbursement/reimbursement_list";
    }

    @RequestMapping("/openreimAdd")
    public String openreimAdd() {
        return "/reimbursement/reimbursement";
    }

    @RequestMapping("/reimbursement")
    public String reimbursement(HttpServletRequest request, Model model) {

        String identityCard = request.getParameter("context1");
        String p = reimbursementService.reimbursement(identityCard);
        if (p != null) {
            Rural rural = reimbursementService.getreimbursementlist(identityCard);
            Disease disease = reimbursementService.getDisease(identityCard);
            model.addAttribute("disease", disease);
            model.addAttribute("rural", rural);
            return "/reimbursement/reimbursement_add";
        }
        JOptionPane.showMessageDialog(null, "抱歉您没有参合", "身份信息", JOptionPane.ERROR_MESSAGE);
        return getReimList(1, 10, request, model);
    }

    @RequestMapping("/reimbursementadd")
    public String reimbursementadd(Reimbursement reimbursement, HttpServletRequest request, Model model) {
        reimbursementService.udaterural(reimbursement);
        reimbursementService.reimbursementadd(reimbursement);
        return getReimList(1, 10, request, model);
    }

    @RequestMapping("/reimbursementdelete")
    public String reimbursementdelete(Integer id, HttpServletRequest request, Model model) {
        reimbursementService.reimbursementdelete(id);
        return getReimList(1, 10, request, model);
    }
}
